package com.wangyang.controller;

import com.wangyang.pojo.Article;
import com.wangyang.pojo.Result;
import com.wangyang.service.ArticleService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Validated
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result addArticle(@RequestBody @Validated(Article.Add.class) Article article){
        return articleService.addArticle(article);
    }

    @GetMapping
    public Result getPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String state){
        return articleService.getArticlePage(pageNum, pageSize, categoryId, state);
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article){
        return articleService.updateArticle(article);
    }

    @GetMapping("/detail")
    public Result getArticleById(@RequestParam("id") Integer id){
        return articleService.getArticleById(id);
    }

    @DeleteMapping
    public Result deleteArticleById(@RequestParam("id") Integer id){
        return articleService.deleteArticle(id);
    }

}
